package cn.fenglaicaimi.controller;

import cn.fenglaicaimi.base.domain.OAuthAccessTokenVo;
import cn.fenglaicaimi.util.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class OAuthController {
	@Value("${wechat.appID}")
	private String appid;
	@Value("${wechat.appsecret}")
	private String appsecret;
	
	@RequestMapping("oauth")
	@ResponseBody
	public String oauth(String code){
		System.out.println(code);
		//获取access_token
		String oauthAccessToken = "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+appid+"&secret="+appsecret+"&code="+code+"&grant_type=authorization_code";
		String oauthRet = HttpUtil.get(oauthAccessToken);
		OAuthAccessTokenVo authAccessTokenVo = JSON.parseObject(oauthRet, OAuthAccessTokenVo.class);
		
		//拉取用户信心
		String userInfoUrl ="https://api.weixin.qq.com/sns/userinfo?access_token="+authAccessTokenVo.getAccess_token()+"&openid=oOyNw08wyQ72EwXuu-k9Bb2NIBmw&lang=zh_CN";
		String user = HttpUtil.get(userInfoUrl);
		return user;
	}
}
